package com.ccjq.shosestore.order.dao.impl;

import com.ccjq.shosestore.core.dbmanager.DbConnection;
import com.ccjq.shosestore.order.bo.orderBo;
import com.ccjq.shosestore.order.bo.orderBoGl;
import com.ccjq.shosestore.order.dao.orderDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class orderDaoImpl implements orderDao {
    Connection conn = null;
    PreparedStatement st = null;
    ResultSet re = null;

    public boolean insertOrderXinXi(orderBo order) {

        String sql = "insert into t_order values(?,GETDATE(),?,?,?,?,?,?,?,?,?,?)";
        conn = DbConnection.getConnextion();
        try {
            st = conn.prepareStatement(sql);
            st.setString(1, order.getOrder_id());
            st.setInt(2, order.getPayment());
            st.setInt(3, order.getAmount());
            st.setString(4, order.getReceiver());
            st.setString(5, order.getProvince());
            st.setString(6, order.getCity());
            st.setString(7, order.getCounty());
            st.setString(8, order.getAddress());
            st.setString(9, order.getZipcode());
            st.setString(10, order.getTelphone());
            st.setString(11, order.getUser_id());
            int a = st.executeUpdate();
            if (a > 0) {
                return true;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(re, st, conn);
        }
        return false;
    }

    public boolean insertOrderGL(String order_rid, String prid, int quantity, int price) {

        boolean b = true;
        String sql = "insert into t_order_product values(?,?,?,?)";
        conn = DbConnection.getConnextion();
        try {
            st = conn.prepareStatement(sql);
            st.setString(1, order_rid);
            st.setString(2, prid);
            st.setInt(3, quantity);
            st.setInt(4, price);
            int reult = st.executeUpdate();
            if (reult > 0) {
                return b;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(re, st, conn);
        }
        return false;
    }

    public boolean updateStock(String prid, int quantity) {
        return false;
    }



    public orderBo selectXinXi(String id) {
        return null;
    }

    public boolean selectquntity(String prid, int newQuntity) {

        String sql = "select quantity from t_stock where product_id=?";
        conn = DbConnection.getConnextion();
        try {
            st = conn.prepareStatement(sql);
            st.setString(1, prid);
            re = st.executeQuery();
            if (re.next()) {
                int oldQuantity = re.getInt("quantity");
                if (oldQuantity >= newQuntity) {
                    return true;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(re, st, conn);
        }
        return false;
    }

    public int Selectprice(String prid) {
        int price = 0;
        String sql = "select price from t_product where product_id=?";
        conn = DbConnection.getConnextion();
        try {
            st = conn.prepareStatement(sql);
            st.setString(1, prid);
            re = st.executeQuery();
            if (re.next()) {
                price = re.getInt("price");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(re, st, conn);
        }

        return price;
    }

    public int UpdateStock(String prid, int newQuantity) {

        int price = 1;
        String sql = "update t_stock set quantity=quantity-? where product_id=?";
        conn = DbConnection.getConnextion();
        try {
            st = conn.prepareStatement(sql);
            st.setInt(1, newQuantity);
            st.setString(2, prid);
            price = st.executeUpdate();
            return price;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(re, st, conn);
        }
        return 0;
    }

    public List<orderBo> selectAll(int startRow, int pageSize) {

        List<orderBo> list = new ArrayList<orderBo>();
        String sql = "select top  " + pageSize + "order_id,create_time,amount  from  t_order WHERE  order_id  not in (select top " + --startRow + " order_id from t_order ) ";
        conn = DbConnection.getConnextion();
        try {
            st = conn.prepareStatement(sql);
            re = st.executeQuery();
            while (re.next()) {
                orderBo bo = new orderBo();
                bo.setOrder_id(re.getString("order_id"));
                bo.setCreate_time(re.getDate("create_time"));
                bo.setAmount(re.getInt("amount"));
                list.add(bo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(re, st, conn);
        }

        return list;
    }

    public int zhs(String userid) {
        int a=0;
        String sql = "select count(*)  from t_order ";
        conn = DbConnection.getConnextion();
        try {
            st = conn.prepareStatement(sql);
            re = st.executeQuery();
            if (re.next()) {
                a=re.getInt(1);
                return a;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(re, st, conn);
        }
        return 0;
    }

    public orderBo dingdanXinXi(String orid) {

        orderBo bo;
        String sql = "select *  from t_order where order_id=?";
        conn = DbConnection.getConnextion();
        try {
            st = conn.prepareStatement(sql);
            st.setString(1,orid);
            re = st.executeQuery();
            if(re.next())
            {
               bo=new orderBo();
               bo.setOrder_id(re.getString(1));
               bo.setCreate_time(re.getDate(2));
               bo.setPayment(re.getInt(3));
               bo.setAmount(re.getInt(4));
               bo.setReceiver(re.getString(5));
               bo.setProvince(re.getString(6));
               bo.setCity(re.getString(7));
               bo.setCounty(re.getString(8));
               bo.setAddress(re.getString(9));
               bo.setZipcode(re.getString(10));
               bo.setTelphone(re.getString(11));
               bo.setUser_id(re.getString(12));
               return bo;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(re, st, conn);
        }
        return null;
    }

    public List<orderBoGl> shangpin(String orid) {

        List<orderBoGl> list=new ArrayList<orderBoGl>();
        String sql = "select a.product, quantity,a.price,description from  t_order_product  a join t_product b on a.product=b.product_id where order_id=?";
        conn = DbConnection.getConnextion();
        try {
            st = conn.prepareStatement(sql);
            st.setString(1,orid);
            re = st.executeQuery();
            if(re.next())
            {
                orderBoGl or=new orderBoGl();
                or.setProductid(re.getString(1));
                or.setQuantity(re.getInt(2));
                or.setPrice(re.getInt(3));
                or.setDescription(re.getString(4));
                list.add(or);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(re, st, conn);
        }
        return list;
    }

    public void deleteShopCar(String userid) {
        List<orderBoGl> list=new ArrayList<orderBoGl>();
        String sql = "delete from t_shopping_cart  where  user_id=?";
        conn = DbConnection.getConnextion();
        try {
            st = conn.prepareStatement(sql);
            st.setString(1,userid);
            st.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(null, st, conn);
        }
    }
}
